home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 November: Tool Chest / Apple_Developer_Group_CD_Series_November_1997_Tool_Chest.iso / Sample Code / Async driver tester / async tester.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-18  |  7.5 KB  |  307 lines  |  [TEXT/CWIE]

  1. #include <Types.h>
  2. #include <QuickDraw.h>
  3. #include <OSUtils.h>
  4. #include <SegLoad.h>
  5. #include <Fonts.h>
  6. #include <Windows.h>
  7. #include <Menus.h>
  8. #include <TextEdit.h>
  9. #include <Dialogs.h>
  10. #include <Processes.h>
  11. #include <Files.h>
  12. #include <Devices.h>
  13. #include <Memory.h>
  14. #include <LowMem.h>
  15. #include <FSM.h>
  16. #include "LinkedList.h"
  17.  
  18. // multiple of 512 bytes.
  19. #define amountToRead    (1024L*1024L)
  20.  
  21. #define kSleepTime        180
  22.  
  23. #define kErrorAlertID    128
  24. #define kStatusAlertID    129
  25. #define kAboutAlertID    150
  26.  
  27. #define kMenuBarID        128
  28. #define kAppleMenuID    128
  29. #define kFileMenuID        129
  30. #define kEditMenuID        130
  31. #define kDriverMenuID    131
  32.  
  33. typedef struct DriverInfo {
  34.     Str255            driverName;
  35.     short            driverRefNum;
  36. } DriverInfo, *DriverInfoPtr;
  37.  
  38. Boolean            gQuitting    = false;
  39. myListPtr        gTheList;
  40.  
  41. static OSErr ToolBoxInit (void)
  42. {    
  43.     MaxApplZone();
  44.     InitGraf (&qd.thePort);
  45.     InitFonts ();
  46.     InitWindows ();
  47.     InitMenus ();
  48.     TEInit ();
  49.     InitDialogs ((long)nil);
  50.     InitCursor ();
  51.  
  52.     return noErr;
  53. }
  54.  
  55. static short TestDriverForAsyncCapability (short refNum, short driveNum) {
  56.     OSErr            err;
  57.     ParamBlockRec    pBlock;
  58.     Ptr                buf            = nil;
  59.     int                count        = 0;
  60.     Str255            errNumStr;
  61.     Boolean            isAsync        = -1;    // use -1 to mean there was an error
  62.     long            size        = amountToRead;
  63.  
  64.     buf = NewPtr (size);
  65.     err = MemError ();
  66.     if (buf == nil) {
  67.         // Couldn't get memory for read buffer
  68.         NumToString (MemError(), errNumStr);
  69.         ParamText ("\pCouldn't get memory for buffer, ", errNumStr, nil, nil);
  70.         (void)StopAlert (kErrorAlertID, nil);
  71.     }
  72.  
  73.     if (buf != nil) {
  74.         // Read from block 0 off the device. 
  75.         pBlock.ioParam.ioCompletion = nil;            // I'll just poll ioResult instead.
  76.         pBlock.ioParam.ioVRefNum = driveNum;        // physical drive number
  77.         pBlock.ioParam.ioRefNum = refNum;
  78.         pBlock.ioParam.ioBuffer = buf;
  79.         pBlock.ioParam.ioReqCount = size;
  80.         pBlock.ioParam.ioPosMode = fsFromStart & noCacheMask;
  81.         pBlock.ioParam.ioPosOffset = 0;
  82.  
  83.         err = PBReadAsync(&pBlock);
  84.     }
  85.  
  86.     if (err < noErr) {
  87.         // Couldn't queue the request type error
  88.         NumToString (err, errNumStr);
  89.         ParamText ("\pCouldn't queue the request, error: ", errNumStr, nil, nil);
  90.         (void)StopAlert (kErrorAlertID, nil);
  91.     } else {
  92.         // Spin while read happens async
  93.         while (pBlock.ioParam.ioResult > 0) {
  94.             count++;
  95.         }
  96.     }
  97.  
  98.     // After read completes, was there an error (bad block, etc.)?
  99.     // This code only runs if PBReadAsync returned noErr so we don't
  100.     // report the same error twice.
  101.     if (err == noErr && pBlock.ioParam.ioResult != noErr) {
  102.         NumToString (pBlock.ioParam.ioResult, errNumStr);
  103.         ParamText ("\pDriver returned an error of: ", errNumStr, nil, nil);
  104.         (void)StopAlert (kErrorAlertID, nil);
  105.     } else if (err == noErr && pBlock.ioParam.ioResult == noErr) {
  106.         if (count != 0) {
  107.             // If we got to spin in the above while loop, driver is async...
  108.             isAsync = 1;
  109.         } else {
  110.             // ...else it isn't async
  111.             isAsync = 0;
  112.         }
  113.     }
  114.  
  115.     // If we have allocated any memory, release it.
  116.     if (buf != nil) {
  117.         DisposePtr (buf);
  118.     }
  119.  
  120.     return isAsync;
  121. }
  122.  
  123. static myListPtr GetDriverList (void) {
  124.     Ptr                UTblBase;
  125.     Ptr                p = (char*)0x01d2;
  126.     DCtlHandle        driverEntry;
  127.     DRVRHeaderPtr    driverHeader;
  128.     long            i;
  129.     short            UTblEntries;
  130.     myListPtr        theList;
  131.     DriverInfoPtr    drvrInfo;
  132.  
  133.     //UTblEntries = LMGetUnitTableEntryCount ();
  134.     //LMGetUnitTableEntryCount doesn't seem to have any glue (can't link), so hit low mem ourselves...
  135.     UTblEntries = *(short*)p;
  136.     UTblBase = LMGetUTableBase ();
  137.  
  138.     theList = NewList ();
  139.  
  140.     if (theList != nil) {
  141.         for (i = 0; i < UTblEntries; i++) {
  142.             driverEntry = (DCtlHandle)((Handle)UTblBase)[i];
  143.             if (driverEntry != nil) {
  144.                 driverHeader = (DRVRHeaderPtr)(*driverEntry)->dCtlDriver;
  145.                 if ((*driverEntry)->dCtlFlags & dRAMBasedMask) {
  146.                     driverHeader = *(DRVRHeaderHandle)driverHeader;
  147.                 }
  148.  
  149.                 // If the driver isn't already open, don't display it
  150.                 if (!((*driverEntry)->dCtlFlags & dOpenedMask)) {
  151.                     driverHeader = nil;
  152.                 }
  153.  
  154.                 if (driverHeader != nil) {
  155.                     drvrInfo = (DriverInfoPtr)NewPtr (sizeof (DriverInfo));
  156.                     if (drvrInfo != nil) {
  157.                         BlockMoveData (&driverHeader->drvrName, drvrInfo->driverName, driverHeader->drvrName[0]+1);
  158.                         drvrInfo->driverRefNum = (*driverEntry)->dCtlRefNum;
  159.                         AppendToList (drvrInfo, theList);
  160.                     }
  161.                 }
  162.             }
  163.         }
  164.     }
  165.  
  166.     return theList;
  167. }
  168.  
  169. static short GetFirstDriveNum (short refNum) {
  170.     QHdrPtr            driveQueue;
  171.     DrvQElPtr        driveQElem;
  172.  
  173.     driveQueue = GetDrvQHdr ();
  174.     driveQElem = (DrvQElPtr)driveQueue->qHead;
  175.  
  176.     while (driveQElem != nil && driveQElem->dQRefNum != refNum) {
  177.         driveQElem = (DrvQElPtr)driveQElem->qLink;
  178.     }
  179.  
  180.     if (driveQElem->dQRefNum == refNum) {
  181.         return driveQElem->dQDrive;
  182.     } else {
  183.         //We didn't find a match for this refNum
  184.         return 0;
  185.     }
  186. }
  187.  
  188. static void DoMenuChoice (long menuChoice) {
  189.     DriverInfoPtr    listElm;
  190.     short            result;
  191.  
  192.     if (menuChoice) {
  193.         short    menu = HiWord (menuChoice),
  194.                 item = LoWord (menuChoice);
  195.         switch (menu) {
  196.             case kAppleMenuID:
  197.                 if (item == 1) {
  198.                     (void)Alert (kAboutAlertID, nil);
  199.                 } else {
  200.                     MenuHandle    appleMenu;
  201.                     Str255        accName;
  202.                     short        accNumber;
  203.  
  204.                     appleMenu = GetMenuHandle (kAppleMenuID);
  205.                     if (appleMenu != nil) {
  206.                         GetMenuItemText (appleMenu, item, accName);
  207.                         accNumber = OpenDeskAcc (accName);
  208.                     }
  209.                 }
  210.                 break;
  211.             case kFileMenuID:
  212.                 gQuitting = true;
  213.                 break;
  214.             case kEditMenuID:
  215.                 //HandleEditChoice (item);
  216.                 break;
  217.             case kDriverMenuID:
  218.                 listElm = (DriverInfoPtr)GetItemNumFromList (item, gTheList);
  219.                 result = TestDriverForAsyncCapability (listElm->driverRefNum, GetFirstDriveNum (listElm->driverRefNum));
  220.                 if (result == 1) {            //Driver is async capable
  221.                     ParamText ("\pasynchronous", nil, nil, nil);
  222.                     (void)NoteAlert (kStatusAlertID, nil);
  223.                 } else if (result == 0) {    //Driver is not async capable
  224.                     ParamText ("\psynchronous", nil, nil, nil);
  225.                     (void)NoteAlert (kStatusAlertID, nil);
  226.                 } else {
  227.                     //status unknown because of error
  228.                 }
  229.                 break;
  230.         }
  231.         HiliteMenu (0);
  232.     }
  233. }
  234.  
  235. void main (void) {
  236.     MenuHandle        theMenu;
  237.     Handle            menuBar;
  238.     DriverInfoPtr    listElm;
  239.     long            i;
  240.  
  241.     ToolBoxInit ();
  242.  
  243.     menuBar = GetNewMBar (kMenuBarID);
  244.     if (menuBar == nil) {                            /* No memory for our menu, can't recover */
  245.         SysBeep (1);
  246.         gQuitting = true;
  247.     } else {
  248.         SetMenuBar (menuBar);
  249.  
  250.         theMenu = GetMenuHandle (kAppleMenuID);
  251.         if (theMenu == nil) {
  252.             SysBeep (1);
  253.             gQuitting = true;
  254.         } else {                                        /* No memory for our menu, can't recover */
  255.             AppendResMenu (theMenu, 'DRVR');
  256.             DrawMenuBar ();
  257.  
  258.             gTheList = GetDriverList ();
  259.  
  260.             // Make the Driver menu by inserting the name of each found driver
  261.             i = 1;
  262.             listElm = (DriverInfoPtr)GetItemNumFromList (i++, gTheList);    // Gets a name from the list
  263.             theMenu = GetMenuHandle (kDriverMenuID);
  264.             while (listElm != nil) {
  265.                 AppendMenu (theMenu, listElm->driverName);
  266.                 listElm = (DriverInfoPtr)GetItemNumFromList (i++, gTheList);
  267.             }
  268.         }
  269.     }
  270.  
  271.     while (gQuitting == false) {
  272.         EventRecord            event;
  273.         Boolean                gotEvent;
  274.         WindowPtr            window;
  275.  
  276.         gotEvent = WaitNextEvent (everyEvent, &event, kSleepTime, nil);
  277.         if (gotEvent == true) {
  278.             short    thePart;
  279.  
  280.             switch (event.what) {
  281.                 case mouseDown:
  282.                     thePart = FindWindow (event.where, &window);
  283.                     if (thePart == inMenuBar) {
  284.                         DoMenuChoice (MenuSelect (event.where));
  285.                     } else if (thePart == inSysWindow) {
  286.                         SystemClick (&event, window);
  287.                     }
  288.                     break;
  289.                 case autoKey:
  290.                 case keyDown:
  291.                     if (event.modifiers & cmdKey)
  292.                         DoMenuChoice (MenuKey (event.message & charCodeMask));
  293.                     break;
  294.                 case activateEvt:
  295.                 case osEvt:
  296.                 case updateEvt:
  297.                 case kHighLevelEvent:
  298.                 case mouseUp:
  299.                 case keyUp:
  300.                 case diskEvt:
  301.                     break;
  302.             }
  303.         }
  304.     }
  305. }
  306.  
  307.